﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class Resource : EntityBase<Resource>
	{
		private Language _language;

		/// <summary>
		/// Initializes a new instance of the <see cref="Resource"/> class.
		/// </summary>
		public Resource()
		{
			DeterminePermission(Roles.Resources);
		}

		/// <summary>
		/// Gets or sets the language id.
		/// </summary>
		/// <value>The language id.</value>
		public virtual int LanguageId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the item key.
		/// </summary>
		/// <value>The item key.</value>
		public virtual string Key
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the item value.
		/// </summary>
		/// <value>The item value.</value>
		public virtual string Value
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the language.
		/// </summary>
		/// <value>The language.</value>
		public virtual Language Language
		{
			get
			{
				return _language;
			}
			set
			{
				_language = value;

				if (_language != null)
				{
					this.LanguageId = _language.Id;
				}
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<Resource> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Key)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Key));

			rules.Add(x => x.Value)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Value));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}